package main

import "fmt"

//#link： https://leetcode-cn.com/problems/string-compression/
func main() {
	b := []byte{'a', 'a', 'b', 'b', 'c', 'c', 'c'}
	fmt.Println(compress(b))
}

func compress(chars []byte) int {
	write, left := 0, 0
	for read, ch := range chars {
		//当读到头 或者不相等的时候
		if read == len(chars)-1 || ch != chars[read+1] {
			//先把当前字符写进去
			chars[write] = ch
			write++
			num := read - left + 1
			//当重复的数字大于1时
			if num > 1 {
				if num >= 10 {
					chars[write] = '0' + byte(num/10)
					write++
					chars[write] = '0' + byte(num%10)
					write++
				} else {
					chars[write] = '0' + byte(num)
					write++
				}
			}
			left = read + 1
		}
	}
	return write
}
